<?php
/**
 * 本页面用于单个采集
 * 仅供测试用 
 */
	include_once('../sys/sys.php');
	header('Content-Type: text/html; charset=utf-8');
	
//	$str = file_get_contents("http://ww.kyo.cn/comic/7175/ttzs01/index.js");
//	echo $str;
	
	$url = $_GET['url'];
	$pic_url_pre = 'http://1.kyo.cn/comic_img/';
	//分离URL以及ID
	$id = explode("/",$url);
	if ($id[count($id) - 1] == "") {
		$id = $id[count($id) - 2];
	}
	else 
		$id = $id[count($id)-1];
	if(!is_numeric($tid) || $tid < 0){
		show_normal_mesR('TAG ID错误','TAG ID错误','TAG查询');
	}
	//定义公用变量
	if (substr($url,-1)!="/")
		$url.="/";
	$url_pre = $url;
		
		
	//获取URL内容
	$content = 	file_get_contents($url);
	$content = 	mb_convert_encoding(file_get_contents($url),"UTF-8","GB2312");
	
	//获取名称
	$name = explode('<div id="mhtitle">',$content);
	$name = $name[1];
	$name = explode('</div>',$name);
	$name = ADD_addslashes($name[0],true);
	//获取最后更新时间
	$dt = explode('最后更新：',$content);
	$dt = $dt[1];
	$dt = explode('</p>',$dt);
	$dt = strtotime(trim($dt[0]));
	
	
	//获取URL列表
	$urls = explode('"comiczj"><ul>',$content);
	$urls = $urls[1];
	$urls = explode('</ul>',$urls);
	$urls = $urls[0];
	$replacer = array('<img src="/images/ar5.gif" />&nbsp;',' target="_blank"','</a>');
	$replacerc = array('','','','');
	$urls = str_replace($replacer,$replacerc,$urls);
	$match_str = "/<a href=\"(.*)\" title=(.*)>/isU";
	preg_match_all($match_str,$urls,$urln);
	$urln = $urln[1];
	//获取每单位总数
	$ulnum = count($urln);
	//进入每页进行处理
	
	//插入分类信息
		//检查当前ID是否存在
		$where = 'AND id= ' . $id .'';
		$result = $DB->build_and_exec_query(
			array('select' => 'COUNT(*) as max','from'   => 'kyotags','where'	=> "1 " . $where ,)
		);	
		//当已经存在的时候更新日期
		if ($result['max'] > 0) {
			
		}
		//当不存在时插入
		else{
			do_taginsert($id,$dt,$name,0);
		}
	//获取数据库COUNT
	$where = 'AND id= ' . $id .'';
		$result = $DB->build_and_exec_query(
			array('select' => 'count','from'   => 'kyotags','where'	=> "1 " . $where ,)
		);	
	$dbcount = $result['count'];
	
/*	//由于是逆序，需要倒装数组
	$urln = array_reverse($urln);*/
	
	
	foreach ($urln as $k=>$v) {
		//当COUNT小于已经采集的时候，跳过。
		if ($k < $dbcount) {
			continue;
		}
		$current_url = $url_pre . $v;
		$comic_contents = mb_convert_encoding(file_get_contents(mb_convert_encoding($current_url,"GB2312","UTF-8")),"UTF-8","GB2312");
		//开始获取总页数以及图片子路径以及总体图片占位符个数
		$picjs = mb_convert_encoding(file_get_contents(mb_convert_encoding($current_url,"GB2312","UTF-8") . "/index.js"),"UTF-8","GB2312");
		$replacer = array('var ',"'"," = ");
		$replacerc = array('',"","=");
		$picjs = str_replace($replacer,$replacerc,$picjs);
		
		$match_str = "/=(.*);/isU";
		preg_match_all($match_str,$picjs,$purls);
		$purls = $purls[1];
		$total_pages = $purls[0];
		$sub_url_string = $purls[1];
		$prez = $purls[2] + 1;
		//生成完整路径
		$complete_url = $pic_url_pre . $sub_url_string;
		//这里添加数据库插入语句
		do_photoinsert($id,$complete_url,$total_pages,$prez);
	}
	//更新计数
	$where = 'id= ' . $id .'';
	$DB->do_update('kyotags',
		array(					  
		   'lastupdate'				=> $dt,
		   'count'					=> count($urln),
		 ),
		 $where
	);
	
		
	
	
	function do_taginsert($id,$lu,$name,$count){
		global $DB;
		$DB->do_insert( 'kyotags', array( 'id'			=> $id,
								   'lastupdate'				=> $lu,
								   'name'					=> $name,
								   'count'					=> $count,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	
	function do_photoinsert($uid,$url,$count,$prez){
		global $DB;
		$DB->do_insert( 'kyophotos', array( 
								   'uid'		=> $uid,
								   'url'		=> $url,
								   'count'		=> $count,
								   'prez'		=> $prez
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	//漫画具体下载地址生成
	
	function make_picurls($base_url,$pages,$prez){
		$urls = "";
		get_pre_nums($prez, 1);
		for ($i = 1; $i < $pages + 1; $i++) {
			$urls .= $base_url . get_pre_nums($prez, $i) . ".jpg\r\n";
		}
		return $urls;
	}
	
	
	
	
	echo "采集完毕";
	
	
	exit;
	
	
?>